<?php
namespace Common\Model;

/**
 * 客栈表
 * @modify trainyao 2015-12-31 创建
 * @index  hotelentity(status,city_id)
 */

Class HotelEntityModel extends \Common\Model\BaseModel
{
    protected $trueTableName = 'tb_hotel_entity';
    
    //获取已经同步的酒店ID
    //trainyao 2017-2-6  创建
    public function getSynHotelIds(){
        $option['communityno'] = array('neq', 0);
        return $this->field('id,communityno,name')->where($option)->select();
    }
    /**
     * 新建客栈
     * @modify trainyao 2015-12-31  创建
     * @modify zhangzhen 2016-12-22  修改 添加集群号
     * @modify wenhui 2017-05-24  添加时区
     * @param  string   $name       客栈名
     * @param  integer   $cityId     城市id
     * @param  integer   $districtId 区id
	 * @param  integer   $communityNo 集群号
     * @return boolean 添加结果
     */
    public function addInfo($name, $cityId, $districtId, $communityNo=NULL, $usage = 2, $enddate = null, $universalTime = '5') {
        $data = array(
            'name' => $name,
            'city_id' => $cityId,
            'district_id' => $districtId,
            'usage' => $usage,
            'enddate' => empty($enddate)?date('Y-m-d H:i:s',strtotime('+1 year')):$enddate,
            'universaltime_id' => $universalTime,
        );
		if (!empty($communityNo)) {
            $data['communityno'] = $communityNo;
        }
        $id = $this->add($data);
        return $id === false ? false : $id;
    }
    
    /**
     * 获取客栈信息
     * @modify trainyao 2015-12-31 创建
     * @param  mixin   $id       客栈id(s)
     * @param  mixin   $selectDisable   是否选择禁用客栈
     * @return array 信息
     */
    public function getInfo($hotelId, $selectDisable = true) {
        if(is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
        }
        $option = array();
        $fields = 'id,name,city_id,district_id,communityno,status,usage,enddate,updatetime,universaltime_id';
        $data = array();
        
        // 根据是否选择禁用客栈构造sql语句
        if ($selectDisable !== true) {
            $option['status'] = DISABLE;
        }
        
        if (strpos($hotelId, ',') !== false) {
            $option['id'] = array('in', $hotelId);
            $data = $this->where($option)->field($fields)->select();
        } else {
            $option['id'] = $hotelId;
            $data = $this->where($option)->field($fields)->find();
        }
        
        return empty($data) ? array() : $data;
    }

    /**
     * 返回所有客栈id
     * @modify trainyao 2016-01-21    创建
     * @return array   客栈ids
     */
    public function getAllId() {
        $data = $this->field('id')->select();
        return empty($data) ? array() : $data;
    }

    /**
     * 根据城市查看酒店信息
     * @modify trainyao 2015-12-31 创建
     * @param  mixed   $cityId    城市id
     * @param  bool   $selectDisable 是否选择禁用客栈
     * @return array
     */
    public function getInfoByCity($cityId, $selectDisable = false) {
        $option = array(
            'status' => NOT_DELETE,
        );

        if (is_array($cityId)) {
            $cityId = implode(',', $cityId);
            $option['city_id'] =  array('in', $cityId);
        } else {
            $option['city_id'] = $cityId;
        }

        if ($selectDisable !== false) {
            $option['status'] = array(
                'in',
                NOT_DELETE . ',' . IS_DELETE
            );
        }
        
        $fields = 'id,name,city_id,district_id,communityno,status';
        $data = $this->where($option)->field($fields)->select();
        
        return empty($data) ? array() : $data;
    }
    
    /**
     * 修改客栈信息
     * @modify trainyao 2015-12-31   创建
     * @param  integer   $hotelId     客栈id
     * @param  string   $name          名称
     * @param  integer   $cityId        城市id
     * @param  integer   $districtId    区域id
     * @param  string   $communityNo   绑定锁掌柜集群编号
     * @param  int   $type   设置订单提示声音状态 0 开启 1 关闭
     * @return boolean 执行结果
     */
    public function saveInfo($hotelId, $name = null, $cityId = null, $districtId = null, $communityNo = null, $type = null, $universalTime = null) {
        $option = array();
        $data = array();
        
        $option['id'] = $hotelId;
        if (!empty($name)) {
            $data['name'] = $name;
        }
        if (!empty($cityId)) {
            $data['city_id'] = $cityId;
        }
        if (!empty($districtId)) {
            $data['district_id'] = $districtId;
        }
        if (!empty($communityNo)) {
            $data['communityno'] = $communityNo;
        }
        if (!is_null($type)) {
            $data['voicestatus'] = $type;
        }
        if (!is_null($universalTime)) {
            $data['universaltime_id'] = $universalTime;
        }
        return $this->where($option)->save($data) === false ? false : true;
    }
    
    /**
     * 禁用/启用客栈
     * @modify trainyao 2015-12-31 创建
     * @modify trainyao 2016-07-28 修改为可同时修改多个客栈
     * @param  mixin   $hotelId   客栈ID(s)
     * @param  integer   $status    需要修改为该状态0=启用,1=禁用
     * @return boolean   执行结果
     */
    public function toggleUse($hotelId, $status) {
        $option = array();
        $data = array();
        if(is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
            $option['id'] = array('in', $hotelId);
        }else{
            $option['id'] = $hotelId;
        }
        $data['status'] = $status;
        
        return $this->where($option)->save($data) === false ? false : true;
    }

     /**
     * 获取所有客栈
     * @modify wenhui 2016-12-30 创建
     * @param  mixin   $selectDisable 是否选择禁用客栈
     * @return array
     */
    public function getInfoAllCity($selectDisable = false) {
        $option = array(
            'status' => NOT_DELETE,
        );
        if ($selectDisable !== false) {
            $option['status'] = array(
                'in',
                NOT_DELETE . ',' . IS_DELETE
            );
        }

        $fields = 'id,name,city_id,district_id,communityno,status';
        $data = $this->where($option)->field($fields)->select();

        return empty($data) ? array() : $data;
    }

    /**
     * 返回所有没有禁用的客栈id
     * @modify trainyao 2016-01-21    创建
     * @return array   客栈ids
     */
    public function getAllIdInfo() {
        $option = array(
            'status' => NOT_DELETE,
        );

        $fields = 'id,name,city_id,district_id,communityno,status';
        $data = $this->where($option)->field($fields)->select();

        return empty($data) ? array() : $data;
    }

	/**
     * 更新客栈到期日期
     * @modify zhangzhen 2016-12-29
     * @param integer $hotelId客栈ID(s)
     * @param date $date 到期日期
     * @return type
     */
    public function setDate($hotelId, $date) {
		if(is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
			$option['id'] = array('in', $hotelId);
        }else{
			$option['id'] = $hotelId;
		}
        $result = $this->where($option)->save(array('enddate' => $date));
        return $result === false ? false : true;
    }
    /**
     * 客栈启用或禁用 通过时间判断是否失效 用updatetime缓存当前到期时间
     * @modify zhangzhen 2016-12-29
     * @param integer $hotelId客栈ID(s)
     * @param date $enddate 到期日期
     * @param date $updatetime 更新时间(暂存到期时间)
     * @return type
     */
    public function setActive($hotelId, $enddate, $updatetime = null) {
        if(is_array($hotelId)) {
            $hotelId = implode(',', $hotelId);
            $option['id'] = array('in', $hotelId);
        }else{
            $option['id'] = $hotelId;
        }
        $data = array(
                'enddate' => $enddate,
            );
        if ($updatetime) {
            $data['updatetime'] = $updatetime;
        }
        $result = $this->where($option)->save($data);
        return $result === false ? false : true;
    }
}
